package com.hnkywl.sts.action.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import com.hnkywl.sts.service.cwgl.kmys.CykmdmysService;
import jxl.Sheet;
import jxl.Workbook;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-17
 * Time: 上午10:40
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/kmys/cykmdmys")
public class CykmdmysAction extends SimpleActionSupport {
    private CykmdmysService cykmdmysService;
    private Cykmdmys cykmdmys;
    private File excel;
    private Long gsId;
    private String kmdm;
    private int page;
    private int rows;
    private Long codeId;
    private List<String> bmList;

    @Action("cykmdmysList")
    public void cykmdmysList(){
        renderJson(cykmdmysService.findCykmdmysList(gsId,kmdm,page,rows));
    }
    @Action("cykmdmys_bmszcontent")
    public String cykmbmsz(){
        return SUCCESS;
    }

    @Action("savePmkmys")
    public void savePmKmys(){
        try{
            cykmdmysService.saveKmys(bmList,codeId);
            renderText("{\"statusCode\":200}");
        }catch (Exception  e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    //库存报价导入
    @Action("import")
    public void import1() {
        try   {
            Workbook book  =  Workbook.getWorkbook(excel);
            //  获得第一个工作表对象
            Sheet sheet  =  book.getSheet(0);
//            cykmdmysService.deleteByGsId(gsId);
            //获取总行数
            int rows = sheet.getRows();
            List<Cykmdmys> list = new ArrayList<Cykmdmys>();
            for(int i =1;i<rows;i++){
                Cykmdmys c = new Cykmdmys();
                String cell  =  sheet.getCell( 0,i).getContents().trim();
                if(!cell.matches("^[1-9]\\d*$")){
                    renderText("{\"statusCode\":\"300\", \"message\":\"导入失败，第" + (i+1) + "行第1列数据出错，CodeID必须为整数\"}");
                    return;
                }
                c.setCodeId(Long.parseLong(cell));
                c.setGsId(gsId);
                c.setYu8cwkmmc(sheet.getCell( 2,i).getContents().trim());
                c.setJqcwkmmc(sheet.getCell( 3,i).getContents().trim());
                c.setKmdm(sheet.getCell( 4,i).getContents().trim());
                list.add(c);
            }
            cykmdmysService.batchSave(list,gsId);
            renderText("{\"statusCode\":\"200\", \"message\":\"导入成功！\"}");
        }   catch  (Exception e)  {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("save_cykmdmys")
    public void save(){
        try{
            cykmdmysService.save(cykmdmys);
            renderJson("{\"statusCode\":200}");
        }catch (Exception  e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":300,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    //税率常用科目代码映射
    @Action("slcykmdmysList")
    public void slcykmdmysList(){
        renderJson(cykmdmysService.findSlCykmdmysList(gsId));
    }


    public List<String> getBmList() {
        return bmList;
    }

    public void setBmList(List<String> bmList) {
        this.bmList = bmList;
    }

    public Long getCodeId() {
        return codeId;
    }

    public void setCodeId(Long codeId) {
        this.codeId = codeId;
    }

    public Cykmdmys getCykmdmys() {
        return cykmdmys;
    }

    public void setCykmdmys(Cykmdmys cykmdmys) {
        this.cykmdmys = cykmdmys;
    }

    public File getExcel() {
        return excel;
    }

    public void setExcel(File excel) {
        this.excel = excel;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getKmdm() {
        return kmdm;
    }

    public void setKmdm(String kmdm) {
        this.kmdm = kmdm;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Autowired
    public void setCykmdmysService(CykmdmysService cykmdmysService) {
        this.cykmdmysService = cykmdmysService;
    }
}
